#include <cassert>
#include <vector>
#include "sqlite.h"

int myhandler(void *arg, int col, char **result, char **field_name)
{
    // 传入回调函数后，其内部按照行数决定回调函数调用次数
    // 其实就是在该函数外层套一个循环：for(int i=0;i<row;i++){myhandler()};
    vector<string> *pvvs = (vector<string> *)arg;
    string tmp;
    for (int i = 0; i < col; i++)
    {
        tmp += result[i];
        tmp += ' ';
    }
    pvvs->push_back(tmp);

    return SQLITE_OK;
}

int main()
{
    // 1.创建/打开数据库文件
    SqliteHelper sql("./test.db");
    assert(sql.open());

    // 2.创建表格式(学生信息为例)
    string sql_ct = "create table if not exists student(sn int primary key, name varchar(32), age int);";
    assert(sql.exec(sql_ct, nullptr, nullptr));

    // 3.1新增数据
    // string sql_add = "insert into student values(1,'张三','18'),(2,'李四',19),(3,'赵五',20);";
    // assert(sql.exec(sql_add, nullptr, nullptr));

    // 3.2删除数据
    // string sql_sub = "delete from student where name = '张三';";
    // assert(sql.exec(sql_sub, nullptr, nullptr));

    // 3.3修改数据
    // string sql_fix = "update student set name = '李四六' where name = '李四';";
    // assert(sql.exec(sql_fix, nullptr, nullptr));

    // 3.4查询打印数据
    string sql_search = "select sn, name, age from student;";
    vector<string> vvs;
    assert(sql.exec(sql_search, myhandler, &vvs));
    for (const auto &str : vvs)
    {
        cout << str << endl;
    }

    // 4.关闭数据库
    sql.close();
    return 0;
}